//
//  NSString+XDLink.m
//  kabu
//
//  Created by XiangDongMBP on 2017/4/19.
//  Copyright © 2017年 East. All rights reserved.
//

#import "NSString+XDLink.h"
#import <UIKit/UIKit.h>

NSString * const kTopicPattern = @"#[^#]+#";
//NSString * const kAtUserPattern = @"@[\u4e00-\u9fa5a-zA-Z0-9_-]{2,33}";   // 这个是网上找到的，不能包括特殊符号
NSString * const kAtUserPattern = @"@[^\\s]+\\s";

@implementation NSString (XDLink)
- (NSAttributedString *)xd_matchTopicString {
    // 1.创建正则表达式
    // #话题#的规则
    NSString *topicPattern = kTopicPattern;
    // @用户 的规则
    NSString *userPattern = kAtUserPattern;
    
    // 2.将多个正则表达式合为一个匹配条件
    NSString *patterns = [NSString stringWithFormat:@"%@|%@", topicPattern, userPattern];
    
    // 2.创建正则类对象
    NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:patterns options:NSRegularExpressionCaseInsensitive error:nil];
    
    // 3.在传进来的字符串中获得符合匹配条件的结果
    NSArray<NSTextCheckingResult *> * resultArray = [regex matchesInString:self options:0 range:NSMakeRange(0, self.length)];
    NSLog(@"匹配结果: %@", resultArray);
    
    // 4.遍历结果，可对被匹配出来的文字做处理(如设置颜色、行距、字体等，使用NSM)
    NSMutableAttributedString *mAStr = [[NSMutableAttributedString alloc] initWithString:self];
    // 汉字作为Url，TTLabel不支持
//    for (NSTextCheckingResult *result in resultArray) {
//        NSString *topicStr = [self substringWithRange:NSMakeRange(result.range.location + 1, result.range.length - 2)];
//        NSDictionary *attsDic = @{NSLinkAttributeName : topicStr};
//        [mAStr addAttributes:attsDic range:result.range];
//    }
    [resultArray enumerateObjectsUsingBlock:^(NSTextCheckingResult * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSString *idxStr = [NSString stringWithFormat:@"%ld", idx];
        [mAStr addAttributes:@{NSLinkAttributeName : idxStr} range:obj.range];
    }];
    
    return mAStr;
}

- (NSArray<NSString *> *)xd_topicStringArray {
    // 1.创建正则表达式
    // #话题#的规则
    NSString *topicPattern = kTopicPattern;
    
    // 2.创建正则类对象
    NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:topicPattern options:NSRegularExpressionCaseInsensitive error:nil];
    
    // 3.在传进来的字符串中获得符合匹配条件的结果
    NSArray<NSTextCheckingResult *> * resultArray = [regex matchesInString:self options:0 range:NSMakeRange(0, self.length)];
    
    NSMutableArray *mArray = [NSMutableArray array];
    for (NSTextCheckingResult *result in resultArray) {
        NSString *topicStr = [self substringWithRange:NSMakeRange(result.range.location + 1, result.range.length - 2)];
        [mArray addObject:topicStr];
    }
    return mArray;
}

- (NSArray<NSString *> *)xd_atUserStringArray {
    // 1.创建正则表达式
    // #话题#的规则
    NSString *topicPattern = kAtUserPattern;
    
    // 2.创建正则类对象
    NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:topicPattern options:NSRegularExpressionCaseInsensitive error:nil];
    
    // 3.在传进来的字符串中获得符合匹配条件的结果
    NSArray<NSTextCheckingResult *> * resultArray = [regex matchesInString:self options:0 range:NSMakeRange(0, self.length)];
    
    NSMutableArray *mArray = [NSMutableArray array];
    for (NSTextCheckingResult *result in resultArray) {
        NSString *topicStr = [self substringWithRange:NSMakeRange(result.range.location + 1, result.range.length - 2)];
        [mArray addObject:topicStr];
    }
    return mArray;
}
- (NSArray<NSString *> *)xd_topicAndAtUserStringArray {
    // 1.创建正则表达式
    // #话题#的规则
    NSString *topicPattern = kTopicPattern;
    // @用户 的规则
    NSString *userPattern = kAtUserPattern;
    
    // 2.将多个正则表达式合为一个匹配条件
    NSString *patterns = [NSString stringWithFormat:@"%@|%@", topicPattern, userPattern];
    
    // 3.创建正则类对象
    NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:patterns options:NSRegularExpressionCaseInsensitive error:nil];
    
    // 4.在传进来的字符串中获得符合匹配条件的结果
    NSArray<NSTextCheckingResult *> * resultArray = [regex matchesInString:self options:0 range:NSMakeRange(0, self.length)];
    
    NSMutableArray *mArray = [NSMutableArray array];
    for (NSTextCheckingResult *result in resultArray) {
        NSString *topicStr = [self substringWithRange:NSMakeRange(result.range.location, result.range.length)];
        [mArray addObject:topicStr];
    }
    return mArray;
}

@end
